#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>


#define EARTH_RADIUS         (6378000)                          /**< Earth's mean radius in km */
#define PI_180       (3.141592653589793/ 180)


char campo[15];
char buffer[100],fecha[10],utc[10];
int ind;


typedef struct
{
    double lat;
    double lon;
}NMEA_pos;

typedef struct
{
    int horas;
    int min;
    int segundos;
    int dia;
    int mes;
    int year; 
    
}NMEA_time;

NMEA_time gpsTime;
double nmea_distance(
        const NMEA_pos posIni,    /**< From position in radians */
        const NMEA_pos posFin       /**< To position in radians */
        );
int time2seg(void);
double degree2radian(double val);
void llenarTime(void);        
int validarMask(char *buffer);        
int tokNMEA(void);


int main(void)
{
    char v[100]="$GPRMC,77.52,T,,M,0.004,N,0.008,K,A*06";
    int mask=validarMask(v);
    NMEA_time gpsTime = {10,10,10,10,10,10};
    printf("\nhora: %.2d:%.2d:%.2d",gpsTime.horas,gpsTime.min,gpsTime.segundos);
    printf("\nhora: %.2d:%.2d:%.2d",gpsTime.horas,gpsTime.min,gpsTime.segundos);
    printf("\n %d",mask);
/*    int a=999999999,i;

    
    strcpy(fecha,"110301");
    strcpy(utc,"045351");    
    llenarTime();
    printf("\n %s \n %s", fecha, utc);

    printf("\nhora: %.2d:%.2d:%.2d",gpsTime.horas,gpsTime.min,gpsTime.segundos);
    printf("\nfecha: %.2d/%.2d/%.2d",gpsTime.dia,gpsTime.mes,gpsTime.year);
for(i = 0;i<150;i++)
 {   a=time2seg()*100 + a;
    printf("\n %d",a);}
   NMEA_pos posIni;
    NMEA_pos posFin;
    double val;
    
    posIni.lat=degree2radian(-72.895);
    posIni.lon=degree2radian(7.89);
    
    posFin.lat= degree2radian(-72.88);
    posFin.lon= degree2radian(7.88);
    
    val=nmea_distance(posIni,posFin);
    printf("\n %f",val);
    
    int i=0,j;  

    strcpy(buffer,"$GPGGA,092725.00,4717.11399,N,00833.91590,E,1,8,1.01,499.6,M,48.0,M,,0*5B");

for(j=0;j<5;j++)
{
 
    ind=0;
    printf("\n %s",buffer);
    while(tokNMEA()==0)
    {
        printf("\n %s",campo);
        getch();
    }
    getch();
}*/

getch();     
return 0;    
}

int tokNMEA(void)
{
    int i=0;
     
    memset(campo,0x00,sizeof(campo));
    
    while(buffer[ind]!=',' && buffer[ind]!='*')
    {
        campo[i] = buffer[ind];
        i++;ind++;

    }
    ind = ind + 1;
    campo[i] = 0;
    if(buffer[ind-1]=='*')
    {
        ind=0;
        return 1;
    }
    else 
        return 0;
     
}       


double nmea_distance(
        const NMEA_pos posIni,      /**< From position in radians */
        const NMEA_pos posFin       /**< To position in radians */
        )
{
    double dist, val;
    
    val = sin(posFin.lat) * sin(posIni.lat) + cos(posFin.lat) * cos(posIni.lat) * cos(posFin.lon - posIni.lon);
    
    if(val <= 1 && val >= -1)
        dist = ((double)EARTH_RADIUS) * acos(val);
    else
        dist = 0;
        
    return dist;
}

double degree2radian(double val)
{ return (val * PI_180); }

double deg2degree(double val)
{
    double deg = ((int)(val / 100));
    val = deg + (val - deg * 100) / 60;
    return val;
}

void llenarTime(void)
{   
    char aux[3];
    
    memcpy(aux, utc, 2);

    if(atoi(aux)<5)
        gpsTime.horas = atoi(aux) + 19;
    else
        gpsTime.horas = atoi(aux) - 5;
        
                
    strncpy(aux, utc + 2, 2);
    gpsTime.min = atoi(aux);
    
    strncpy(aux, utc + 4, 2);
    gpsTime.segundos = atoi(aux);

    strncpy(aux, fecha,2);
    gpsTime.dia = atoi(aux);
    
    strncpy(aux, fecha + 2, 2);
    gpsTime.mes=atoi(aux);
    
    strncpy(aux, fecha + 4, 2);
    gpsTime.year = atoi(aux); 
       
}    
int time2seg(void)
{
	int val = 3600*gpsTime.horas + 60*gpsTime.min + gpsTime.segundos;
	return val;	
}
int validarMask(char *buffer)
{
	if(memcmp(buffer, "$GPGGA", 6) == 0)
		return 1;
	else if(memcmp(buffer, "$GPGLL", 6) == 0)
		return 2;
	else if(memcmp(buffer, "$GPGSA", 6) == 0)
		return 3;
	else if(memcmp(buffer, "$GPGSV", 6) == 0)
		return 4;
	else if(memcmp(buffer, "$GPRMC", 6) == 0)
		return 5;
	else if(memcmp(buffer, "$GPVTG", 6) == 0)
		return 6;
	else 
		return 0;
}
